/**
 * 商品成本管理相关js文件
 */
var goodsCostList; // dataTable
var quickSearchText = "";

/**
 * 页面初期化
 */
$(document).ready(function() {
	quickSearchText = $("#quickSearch").val();
	/**
     * 初始化列表
     */
	getList();
});
// 初始化库存单表格
function getList() {
    if(!$.fn.DataTable.isDataTable('#goodsCostList')){
        goodsCostList = $("#goodsCostList").DataTable({
            // "scrollX" : "100%",
            // "scrollY" : "410px",
            "scrollCollapse" : false,
            "processing" : true,
            "serverSide" : true,
            "lengthMenu" : [ [ 10, 25, 50, 100, 500, 1000 ], [ 10, 25, 50, 100, 500, 1000 ] ],
            "dom" : '<"bottom"rtflp>',
            "searching" : false,
            "pagingType" : "full_numbers",
            "deferRender" : true,
            "ordering": false,
    	    "ajax" : {
    	        "url" : "/goodsCost/getGoodsCostList.action",//
    	        "data" : function(d) {
    		        d.quickSearch = encodeURI(quickSearchText);
    	        }
    	    },
    	    "tableTools" : {
    		    "sRowSelect" : "bootstrap"
    	    },
    	    "columnDefs" : [{
    	            "targets" : [ 0 ]
    	        },{
            	"className" : "truncate-text tooltip-column-detail text-left",
                "targets" : [ 1 ]
            },{
            	"className" : "truncate-text tooltip-column-detail text-left",
                "targets" : [ 2 ]
            },{
            	"className" : "truncate-text tooltip-column-detail text-left",
                "targets" : [ 3 ]
            },{
            	"className" : "truncate-text tooltip-column-detail text-left",
                "targets" : [ 4 ]
            },{
            	"className" : "truncate-text tooltip-column-detail text-right",
                "targets" : [ 5 ]
            },{
                "targets" : [ 6 ]
            } ],
    	    "columns" : [ {
                "data" : "sonShow",
                "orderable" : false
            }, {
    	        "data" : "goodsCode",
                "orderable" : false
    	    }, {
    	        "data" : "goodsName",
    	        "orderable" : false
    	    }, {
    	        "data" : "skuCode",
    	        "orderable" : false
    	    }, {
    	        "data" : "skuName",
    	        "orderable" : false
    	    }, {
                "data" : "costPrice",
                "render": numberParser,
                "orderable" : false
            }, {
                "data" : "operation",
                "orderable" : false
            } ],
    	}).on('draw.dt',function() {
//    		 $(".dataTables_scrollHeadInner").css("margin","0 auto");
    		// 解决因滚动条导致的列对不齐问题
             $(".dataTables_scrollBody").css("overflow-y","overlay");
    		
    	});
    }else{
    	    var optionData = goodsCostList.settings()[0].ajax.data;
            optionData = function(d){
                d.quickSearch = encodeURI(quickSearchText);
            }
            goodsCostList.settings()[0].ajax.data = optionData;
            goodsCostList.ajax.reload();
	}   
	$("#goodsCostList").tooltip({
        selector: ".tooltip-column-detail",
        container: "body",
        title: function() {
            if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                return $(this).text();
            }
        }
    });
};

// 商品成本变更列表
$('#goodsCostList tbody').on('click', 'td .fadu', function () {
    // 取得主键
    var goodsId = $(this).attr('goodsId');
    var skuId = $(this).attr('skuId');
    var tr = $(this).closest('tr');
    var row = goodsCostList.row(tr);
    if ( row.child.isShown() ) {
        // This row is already open - close it
        row.child.hide();
        tr.removeClass('shown');
        $(this).removeClass('fa-chevron-up');
        $(this).addClass('fa-chevron-down');
    }
    else {
        // 关闭其他
        $('#goodsCostList tbody tr').each(function(){
            if ($(this).attr("role")=="row"){
                goodsCostList.row($(this)).child.hide();
            }
        });
        $('.fa-chevron-up').addClass('fa-chevron-down');
        $('.fa-chevron-down').removeClass('fa-chevron-up');
        // 生成datatable的HTML代码
        // 判断相应的id的datatable是否存在
        if ($('#goodsCostList' + skuId).length <= 0) {
            var temp = '';
            temp += '<table id="goodsCostList' + skuId + '"';
            temp += 'style="table-layout:fixed;margin-left: 0px;" class="table table-hover table-striped table-bordered table-advanced" width="100%"';
            temp += 'width="100%">';
            temp += '<thead>';
            temp += '<tr>';
            temp += '<th width="15%" colValue="updateTime">变更时间</th>';
            temp += '<th width="15%" colValue="operType">变更原因</th>';
            temp += '<th width="15%" colValue="operMode">变更方式</th>';
            temp += '<th width="10%" colValue="curtCost" style="text-align:right">变更后成本价</th>';
            temp += '<th width="10%" colValue="sourceCode">来源单据编码</th>';
            temp += '<th width="10%" colValue="operUserName">操作人</th>';
            temp += '<th width="25%" colValue="remark">备注</th>';
            temp += '</tr>';
            temp += '</thead>';
            temp += '<tbody>';
            temp += '</tbody>';
            temp += '</table>';
            
            // Open this row
            row.child(temp).show();
            tr.addClass('shown');
            var childTable = new Object();
            childTable.id = "goodsCostList" + skuId;
            childTable.table = $('#goodsCostList' + skuId).DataTable({
                // "scrollX" : "100%",
                "scrollY" : "410px",
                "scrollCollapse" : false,
                "bPaginate": false,
                "processing": true,
                "serverSide": true,
                "dom": '<"bottom"rtfli>',
                "searching": false,
                "deferRender": true,
                "info": false,
                "ordering": false,
                "ajax": {
                    "url": "/goodsCost/showCostChangeList.action?goodsId="+goodsId+"&skuId="+skuId
                },
                "tableTools": {
                    "sRowSelect": "bootstrap"
                },
                "columnDefs" : [ {
                    "className" : "truncate-text tooltip-column-detail text-left",
                    "targets" : [ 0 ]
                },{
                    "className" : "truncate-text tooltip-column-detail text-left",
                    "targets" : [ 1 ]
                },{
                    "className" : "truncate-text tooltip-column-detail text-left",
                    "targets" : [ 2 ]
                },{
                    "className" : "truncate-text tooltip-column-detail text-right",
                    "targets" : [ 3 ]
                },{
                    "className" : "truncate-text tooltip-column-detail text-left",
                    "targets" : [ 4 ]
                },{
                    "className" : "truncate-text tooltip-column-detail text-left",
                    "targets" : [ 5 ]
                },{
                    "className" : "truncate-text tooltip-column-detail text-left",
                    "targets" : [ 6 ]
                }],
                "columns": [{
                    "data": "updateTime",
                    "orderable" : false
                }, {
                    "data": "operType",
                    "orderable" : false
                }, {
                    "data": "operMode",
                    "orderable" : false
                }, {
                    "data": "curtCost",
                    "className":"text-right",
                    "render": numberParser,
                    "orderable" : false
                }, {
                    "data": "sourceCode",
                    "orderable" : false
                },{
                    "data": "operUserName",
                    "orderable" : false
                }, {
                    "data": "remark",
                    "orderable" : false
                }],
            }).on('draw.dt',function() {
                // 解决因滚动条导致的列对不齐问题
                 $(".dataTables_scrollBody").css("overflow-y","overlay");
                
            });
        } else {
            tr.addClass('shown');
        }
        $(this).addClass('fa-chevron-up');
        $(this).removeClass('fa-chevron-down');
        
        $('#goodsCostList' + skuId).tooltip({
            selector: ".tooltip-column-detail-5,.tooltip-column-detail",
            container: "body",
            title: function() {
                if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                    return $(this).text();
                }
            }
        });
    }
});

// 变更成本
function updateCost(skuId) {
    $('#eventTitle').html("变更成本");
    var skuId = skuId;
    var iframeSrc = "./views/goodsCost/changeGoodsCost.html?skuId=" + skuId;
    $("#ifmCostDetail").attr("src", iframeSrc);
    $("#btnRuleDetailSave").unbind("click").bind("click", function() {       
        // 调用子页面的保存方法
        window.top.frames["ifmCostDetail"].window.saveCostChange(window.top.frames["ifmCostDetail"].contentWindow);
    });
    var addEventDialog = window.top.window.borrowCustomModalDialog($("#changeCostWindow"));
    addEventDialog.on('hide.bs.modal', function() {
        // 父窗体归还本页的编辑对话框
        window.top.window.returnCustomModalDialog();
    });
    addEventDialog.modal({
        show : true,
        backdrop : 'static'
    });

}

// 关闭变更成本页面
$('#changeCostWindow').on('hide.bs.modal', function() {
    window.top.window.returnCustomModalDialog();
});


// 快速查询
$("#btnQuickSearch").on('click', function() {
	quickSearchText = $("#quickSearch").val();
	getList();
})

// 搜索框的回车事件
$('#quickSearch').keydown(function(e){
    if(e.keyCode==13){
        $("#btnQuickSearch").click();
    }
});

// 刷新
$("#btnRefresh").on('click', function() {  
    $('#quickSearch').val("");
    quickSearchText = "";
    getList();

})

// 导出
function exportGoodsCost(){
    var xval = window.top.window.getIndexBusyOverlay();
    if(xval) {
        xval.settext("成本导出中......");
    }
    var fileName = "成本导出";
    var sql = "exportGoodsCost";
   
    //特殊情况（附加导出字段）
    //主列表字段
    var obj = new Object(); 
    obj.name = "商品商家编码";
    obj.value = "goodsCode";
    var obj1 = new Object();    
    obj1.name = "商品名称";
    obj1.value = "goodsName";
    var obj2 = new Object();    
    obj2.name = "规格商家编码";
    obj2.value = "skuCode";
    var obj3 = new Object();    
    obj3.name = "规格名称";
    obj3.value = "skuName";
    var obj4 = new Object();    
    obj4.name = "成本价";
    obj4.value = "costPrice";
    //明细列表字段
    var obj5 = new Object();    
    obj5.name = "变更时间";
    obj5.value = "updateTime";
    var obj6 = new Object();    
    obj6.name = "变更原因";
    obj6.value = "operType";
    var obj7 = new Object();    
    obj7.name = "变更方式";
    obj7.value = "operMode";
    var obj8 = new Object();    
    obj8.name = "变更后成本价";
    obj8.value = "curtCost";
    var obj9 = new Object();    
    obj9.name = "来源单据编码";
    obj9.value = "sourceCode";
    var obj10 = new Object();    
    obj10.name = "操作人";
    obj10.value = "operUserName";
    var obj11 = new Object();    
    obj11.name = "备注";
    obj11.value = "remark";
       
    var ob = [obj,obj1,obj2,obj3,obj4,obj5,obj6,obj7,obj8,obj9,obj10,obj11];
    extraFieldJson = JSON.stringify(ob);
    setTimeout(function() {
        xval.remove();
        exportList('','/goodsCost/exportCheck.action','/goodsCost/export.action',sql,fileName,'',extraFieldJson)
    })
}

// 价格2位小数展示
var numberParser = function(data){
    var returnValue = "-";
    var numberData = parseFloat(data);
    if(numberData===+numberData){
        returnValue = numberData.toFixed(2);
    }
    return returnValue;
};


